<?php

namespace common\components\behaviors;

use yii\base\Behavior;
use common\models\BaseSupplierGoods;
use common\models\BaseGoods;
use common\models\BaseColor;
use common\models\BaseType;
use common\models\BaseImg;
use common\models\BaseSuppAvailGoods;
use common\models\YdSyncLog;
use common\models\BaseBrand;
use common\models\SysSupplier;
use common\models\BaseSuppGoodsSite;
use common\models\BasePhoto;
use common\models\BaseLogisticsInterfaceLog;

class SupplierGoodsBehavior extends Behavior{
	public function events() {
		return [
			BaseSupplierGoods::EVENT_PRICE_CHANGE => 'price',
			BaseSupplierGoods::EVENT_NUM_AVAI_CHANGE => 'numAvai',
			BaseSupplierGoods::EVENT_ON_SALE_CHANGE => 'onSale',
		    BaseSupplierGoods::EVENT_ON_ENABLE_CHANGE => 'enableChange',
		    
		    /**物流接口***/
		    BaseSupplierGoods::EVENT_NAME_CHANGE => 'name',
		    BaseSupplierGoods::EVENT_SUPP_CHANGE => 'supp',
		    BaseSupplierGoods::EVENT_TYPE_CHANGE => 'type',
		    BaseSupplierGoods::EVENT_UNIT_CHANGE => 'unit',
		    BaseSupplierGoods::EVENT_BRAND_CHANGE => 'brand',
		    BaseSupplierGoods::EVENT_COLOR_CHANGE => 'color',
		];
	}
	
	/**
	 * @Title: price
	 * @Description: 供应商商品价格修改 
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2015-1-20下午10:29:12
	 */
	public function price($event) {
		$goods = $this->owner;
		if ($goods->status == 1) {
			$post_data = [
							'token'=> md5(HBID.CONFUSE.date('Ymd', time())),
							'hbid' => HBID,
							'act' => 4,
							'goods' => [[	
											'sku'  => $goods->id,
											'wholesale_price' => $goods->price,
											'stock' => $goods->num_avai,
									   ],]
						 ];
			
			$result = http_post_data(YD_URL, $post_data);
			YdSyncLog::insertLog($result, $goods, 2);
			
		}	
	}
	
	/**
	 * @Title: numAvai
	 * @Description: 供应商库存修改 
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2015-1-20下午10:29:30
	 */
	public function numAvai($event) {
		$goods = $this->owner;
		if ($goods->status == 1) {
			
			$post_data = [
							'token'=> md5(HBID.CONFUSE.date('Ymd', time())),
							'hbid' => HBID,
							'act' => 3,
							'goods' =>[[
											'sku'  => $goods->id,
											'wholesale_price' => $goods->price,
											'stock' => $goods->num_avai,
										],]
						];
				
			$result = http_post_data(YD_URL, $post_data);
			YdSyncLog::insertLog($result, $goods, 3);
				
		}
	}
	
	/**
	 * @Title: onSale
	 * @Description:商品上下架修改 
	 * @return: return_type
	 * @author: kai.gao
	 * @date: 2015-1-20下午10:29:45
	 */
	public function onSale($event) {
		$goods = $this->owner;
		if ($goods->status == 1) { //商品上架，通知yd增量添加商品
			$data = BaseSuppGoodsSite::find()->where(['supplier_goods_id'=>$goods->id,'status'=>1])->asArray()->all();
			
			$site = [];
			foreach ($data as $val){
			    if(!empty(f_p('site_params_other')[$val['site_id']])){
			        $site[] = f_p('site_params_other')[$val['site_id']];
			    }  
			}
			
			if(!empty($site)){
				
				$post_data = [
					'token'=> md5(HBID.CONFUSE.date('Ymd', time())),
					'hbid' => HBID,
					'action' => 'sync_add',
					'act' => 1,
					'site' =>$site,
					'goods' => [[
									'base_id'=>$goods->base_id,
									'type' => BaseType::getTypeName($goods->type_id),
									'brand'=>BaseBrand::getBrandName($goods->brand_id),
									'channel'=>SysSupplier::getChannelName($goods->supplier_id),
									'name' => $goods->name,
									'sku'  => $goods->id,
									'description' => BaseGoods::getContent($goods->base_id),
									'detail' => $goods->tips,
									'litpic' => IMAGE_URL.'/'.BaseImg::getImageUrlById($goods->img_id),
									'config_name' => BaseColor::getColor($goods->color).BaseSuppAvailGoods::get_yd_extend($goods->base_id),
									'good_price' => $goods->market_price,
									'wholesale_price' => $goods->price,
									'goods_type' => !empty( $goods->color) ? 1 : 0,
									'stock' => $goods->num_avai,
									'goodsimg' => BasePhoto::getPhotoUrl($goods->base_id),
								],]
				];
				
				$result = http_post_data(YD_URL, $post_data);
				YdSyncLog::insertLog($result, $goods, 4);
			}
			
			
			
		} else {
			//商品下架，通知yd删除商品
			$post_data = [
							'token'=> md5(HBID.CONFUSE.date('Ymd', time())),
							'hbid' => HBID,
							'act' => 3,
							'goods' => [[
											'sku'  => $goods->id,
											'wholesale_price' => $goods->price,
											'stock' => $goods->num_avai,
									   ],]
						 ];

			$result = http_post_data(YD_URL, $post_data);
			YdSyncLog::insertLog($result, $goods, 5);
		}
	}
	
	
	/**
	 * @decription:商品禁用通知云店下架
	 * @return: return_type
	 * @author: weilong.yin
	 * @date: 2015年6月15日下午6:14:51
	 * @modified_date: 2015年6月15日下午6:14:51
	 * @modified_user:	weilong.yin
	*/
	public function enableChange($event){
	    $goods = $this->owner;
	    if($goods->enable==-1){
	        //商品禁用，通知yd删除商品
	        $post_data = [
	            'token'=> md5(HBID.CONFUSE.date('Ymd', time())),
	            'hbid' => HBID,
	            'act' => 3,
	            'goods' => [[
	                'sku'  => $goods->id,
	                'wholesale_price' => $goods->price,
	                'stock' => $goods->num_avai,
	            ],]
	        ];
	        
	        $result = http_post_data(YD_URL, $post_data);
	        YdSyncLog::insertLog($result, $goods, 5);
	    }
	}
	
	
	
	/**
	 * 
	 * @Title: name
	 * @Description: 商品名称修改
	 * @return: return_type
	 * @author: yulong.wang
	 * @date: 2015-4-10下午2:43:37
	 */
	
	public function name($event) {
	    $goods = $this->owner;
	    $supplier = SysSupplier::findOne($goods->supplier_id);
	    if ($supplier) {
    	    if ($supplier->ifzc == 1) {
        	    if ($goods->type_id == 1 || $goods->type_id == 2) {
        	        $imei = 1;
        	        $kind = 'PHONE';
        	    } else if ($goods->type_id == 22) {
        	        $imei = 1;
        	        $kind = 'PAD';
        	    } else {
        	        $imei = 0;
        	        $kind = 'PJ';
        	    }
        	    $brand = BaseBrand::getBrandName($goods->brand_id);
        	    $color = BaseColor::getColor($goods->color);
        	    BaseLogisticsInterfaceLog::getLogisticsProduct($goods->id,$goods->code,$goods->name,$goods->supplier_id,$imei,$kind,$goods->unit,$brand,$color);
    	    }
	    }
	}
	
	
	
	/**
	 *
	 * @Title: supp
	 * @Description: 商品供应商修改
	 * @return: return_type
	 * @author: yulong.wang
	 * @date: 2015-4-10下午2:43:37
	 */
	
	public function supp($event) {
	    $goods = $this->owner;
	    $supplier = SysSupplier::findOne($goods->supplier_id);
	    if ($supplier) {
	        if ($supplier->ifzc == 1) {
        	    if ($goods->type_id == 1 || $goods->type_id == 2) {
        	        $imei = 1;
        	        $kind = 'PHONE';
        	    } else if ($goods->type_id == 22) {
        	        $imei = 1;
        	        $kind = 'PAD';
        	    } else {
        	        $imei = 0;
        	        $kind = 'PJ';
        	    }
        	    $brand = BaseBrand::getBrandName($goods->brand_id);
        	    $color = BaseColor::getColor($goods->color);
        	    BaseLogisticsInterfaceLog::getLogisticsProduct($goods->id,$goods->code,$goods->name,$goods->supplier_id,$imei,$kind,$goods->unit,$brand,$color);
	        }
	    }
	}
	
	
	/**
	 *
	 * @Title: type
	 * @Description: 商品类型修改
	 * @return: return_type
	 * @author: yulong.wang
	 * @date: 2015-4-10下午2:43:37
	 */
	
	public function type($event) {
	    $goods = $this->owner;
	    $supplier = SysSupplier::findOne($goods->supplier_id);
	    if ($supplier) {
	        if ($supplier->ifzc == 1) {
        	    if ($goods->type_id == 1 || $goods->type_id == 2) {
        	        $imei = 1;
        	        $kind = 'PHONE';
        	    } else if ($goods->type_id == 22) {
        	        $imei = 1;
        	        $kind = 'PAD';
        	    } else {
        	        $imei = 0;
        	        $kind = 'PJ';
        	    }
        	    $brand = BaseBrand::getBrandName($goods->brand_id);
        	    $color = BaseColor::getColor($goods->color);
        	    BaseLogisticsInterfaceLog::getLogisticsProduct($goods->id,$goods->code,$goods->name,$goods->supplier_id,$imei,$kind,$goods->unit,$brand,$color);
	        }
	    }
	}
	
	
	
	/**
	 *
	 * @Title: unit
	 * @Description: 商品单位修改
	 * @return: return_type
	 * @author: yulong.wang
	 * @date: 2015-4-10下午2:43:37
	 */
	
	public function unit($event) {
	    $goods = $this->owner;
	    $supplier = SysSupplier::findOne($goods->supplier_id);
	    if ($supplier) {
	        if ($supplier->ifzc == 1) {
        	    if ($goods->type_id == 1 || $goods->type_id == 2) {
        	        $imei = 1;
        	        $kind = 'PHONE';
        	    } else if ($goods->type_id == 22) {
        	        $imei = 1;
        	        $kind = 'PAD';
        	    } else {
        	        $imei = 0;
        	        $kind = 'PJ';
        	    }
        	    $brand = BaseBrand::getBrandName($goods->brand_id);
        	    $color = BaseColor::getColor($goods->color);
        	    BaseLogisticsInterfaceLog::getLogisticsProduct($goods->id,$goods->code,$goods->name,$goods->supplier_id,$imei,$kind,$goods->unit,$brand,$color);
	        }
	    }
	}
	
	
	
	
	/**
	 *
	 * @Title: brand
	 * @Description: 商品品牌修改
	 * @return: return_type
	 * @author: yulong.wang
	 * @date: 2015-4-10下午2:43:37
	 */
	
	public function brand($event) {
	    $goods = $this->owner;
	    $supplier = SysSupplier::findOne($goods->supplier_id);
	    if ($supplier) {
    	    if ($supplier->ifzc == 1) {
        	    if ($goods->type_id == 1 || $goods->type_id == 2) {
        	        $imei = 1;
        	        $kind = 'PHONE';
        	    } else if ($goods->type_id == 22) {
        	        $imei = 1;
        	        $kind = 'PAD';
        	    } else {
        	        $imei = 0;
        	        $kind = 'PJ';
        	    }
        	    $brand = BaseBrand::getBrandName($goods->brand_id);
        	    $color = BaseColor::getColor($goods->color);
        	    BaseLogisticsInterfaceLog::getLogisticsProduct($goods->id,$goods->code,$goods->name,$goods->supplier_id,$imei,$kind,$goods->unit,$brand,$color);
    	    }
	    }
	}
	
	
	
	/**
	 *
	 * @Title: color
	 * @Description: 商品颜色修改
	 * @return: return_type
	 * @author: yulong.wang
	 * @date: 2015-4-10下午2:43:37
	 */
	
	public function color($event) {
	    $goods = $this->owner;
	    $supplier = SysSupplier::findOne($goods->supplier_id);
	    if ($supplier) {
    	    if ($supplier->ifzc == 1) {
        	    if ($goods->type_id == 1 || $goods->type_id == 2) {
        	        $imei = 1;
        	        $kind = 'PHONE';
        	    } else if ($goods->type_id == 22) {
        	        $imei = 1;
        	        $kind = 'PAD';
        	    } else {
        	        $imei = 0;
        	        $kind = 'PJ';
        	    }
        	    $brand = BaseBrand::getBrandName($goods->brand_id);
        	    $color = BaseColor::getColor($goods->color);
        	    BaseLogisticsInterfaceLog::getLogisticsProduct($goods->id,$goods->code,$goods->name,$goods->supplier_id,$imei,$kind,$goods->unit,$brand,$color);
    	    }
	    }
	}
}

?>